package com.xr.common.config;

import com.baomidou.mybatisplus.core.handlers.MetaObjectHandler;
import com.baomidou.mybatisplus.extension.plugins.MybatisPlusInterceptor;
import com.baomidou.mybatisplus.extension.plugins.inner.OptimisticLockerInnerInterceptor;
import com.baomidou.mybatisplus.extension.plugins.inner.PaginationInnerInterceptor;
import com.xr.common.redis.RedisCacheService;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Primary;

import javax.annotation.Resource;

/**
 * MybatisPlus拦截器配置类
 *
 * @author fzh
 */
@Configuration
public class MybatisPlusConfig {

	@Resource
	private RedisCacheService redisCacheService;

	@Bean
	@Primary
	public MybatisPlusInterceptor mpi() {
		MybatisPlusInterceptor mpi = new MybatisPlusInterceptor(); // 1、new一个MP拦截器对象
		mpi.addInnerInterceptor(new PaginationInnerInterceptor()); // 2、添加分页拦截器
		mpi.addInnerInterceptor(new OptimisticLockerInnerInterceptor()); // 3、添加乐观锁拦截器
		return mpi; // 4、返回拦截器对象
	}

	@Bean
	@Primary
	public MetaObjectHandler metaObjectHandler() {
		return new MybatisPlusMetaObjectHandler(redisCacheService);
	}

}
